package com.example.capsule_kafka.kafka;

import com.example.capsule_kafka.bean.GroupMessageBean;
import com.example.capsule_kafka.bean.MessageBean;
import com.example.capsule_kafka.bean.OneToOneMessageBean;
import com.example.capsule_kafka.bean.UserInfoBean;
import com.example.capsule_kafka.configuration.Constant;
import com.example.capsule_kafka.configuration.KafkaConstant;
import com.example.capsule_kafka.mapper.GroupMessageMapper;
import com.example.capsule_kafka.mapper.OneToOneMessageMapper;
import com.example.capsule_kafka.mapper.UserInfoMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/4 19:25
 */
@Component
@Slf4j
public class MessageTypeConsumer {

    @Resource
    GroupMessageMapper groupMessageMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    OneToOneMessageMapper oneToOneMessageMapper;

    @KafkaListener(topics = KafkaConstant.DELETE_MESSAGE_TOPIC)
    public void deleteMessage(String records) {

        List<Integer> ids = new Gson().fromJson(records, new TypeToken<List<Integer>>() {
        }.getType());

        if (ids != null && ids.size() > 0) {
            groupMessageMapper.deleteBatchIds(ids);
            log.info("删除完毕！");
        }
    }

    @KafkaListener(topics = KafkaConstant.SAVE_MESSAGE_TOPIC)
    public void saveMessage(String record) {

        MessageBean messageBean = new Gson().fromJson(record, MessageBean.class);

        if (messageBean != null) {
            if (!messageBean.isOneToOne()) {
                saveGroupMessage(messageBean);
            } else {
                saveOneToOneMessage(messageBean);
            }

        }

    }

    private void saveOneToOneMessage(MessageBean messageBean) {
        OneToOneMessageBean oneToOneMessageBean = new OneToOneMessageBean();

        if (messageBean.getMsgType() != 0) {
            String pre = messageBean.getMsgType() == 1 ? Constant.IMAGE_PATH + "/" : (messageBean.getMsgType() == 2) ? Constant.FILE_PATH : Constant.VIDEO_PATH;
            if (!new File(pre).exists()) {
                new File(pre).mkdirs();
            }
            if (messageBean.getMsgType() != 1) {
                oneToOneMessageBean.setFileSize(messageBean.getFileSize());
            }
            oneToOneMessageBean.setContent(pre + messageBean.getFileName());
            oneToOneMessageBean.setFilePath(messageBean.getFilePath());
        } else {
            oneToOneMessageBean.setContent(messageBean.getContent());
        }
        if (messageBean.getSenderAvatarPath() != null && messageBean.getSenderAvatarPath().contains("/")) {
            UserInfoBean userInfoBean = userInfoMapper.selectById(messageBean.getSenderId());
            if (userInfoBean != null) {
                oneToOneMessageBean.setSenderAvatarServerPath(userInfoBean.getAvatarServerPath());
            }
        } else if (messageBean.getSenderAvatarPath() != null) {
            oneToOneMessageBean.setSenderAvatarServerPath("");
        }
        oneToOneMessageBean.setMsgType(messageBean.getMsgType());
        oneToOneMessageBean.setSenderName(messageBean.getSenderName());
        oneToOneMessageBean.setSenderId(messageBean.getSenderId());
        oneToOneMessageBean.setReceiverId(messageBean.getReceiverId());
        oneToOneMessageBean.setRoomName(messageBean.getRoomName());
        oneToOneMessageBean.setSendTime(Timestamp.valueOf(messageBean.getSendTime()));
        oneToOneMessageBean.setField(messageBean.getField());
        oneToOneMessageBean.setSenderAvatarLocalPath(messageBean.getSenderAvatarPath());
        oneToOneMessageBean.setSignStatus(messageBean.getStatus());

        oneToOneMessageMapper.insert(oneToOneMessageBean);
    }

    private void saveGroupMessage(MessageBean messageBean) {
        GroupMessageBean groupMessageBean = new GroupMessageBean();
        groupMessageBean.setMsgType(messageBean.getMsgType());
        groupMessageBean.setField(messageBean.getField());
        groupMessageBean.setSenderName(messageBean.getSenderName());
        if (messageBean.getMsgType() != 0) {
            String pre = messageBean.getMsgType() == 1 ? Constant.IMAGE_PATH + "/" : (messageBean.getMsgType() == 2) ? Constant.FILE_PATH : Constant.VIDEO_PATH;
            if (!new File(pre).exists()) {
                new File(pre).mkdirs();
            }
            if (messageBean.getMsgType() != 1) {
                groupMessageBean.setFileSize(messageBean.getFileSize());
            }
            groupMessageBean.setContent(pre + messageBean.getFileName());
            groupMessageBean.setFilePath(messageBean.getFilePath());
        } else {
            groupMessageBean.setContent(messageBean.getContent());
        }
        if (messageBean.getSenderAvatarPath() != null && messageBean.getSenderAvatarPath().contains("/")) {
            UserInfoBean userInfoBean = userInfoMapper.selectById(messageBean.getSenderId());
            if (userInfoBean != null) {
                groupMessageBean.setSenderAvatarServerPath(userInfoBean.getAvatarServerPath());
            }
        } else if (messageBean.getSenderAvatarPath() != null) {
            groupMessageBean.setSenderAvatarServerPath("");
        }
        groupMessageBean.setSenderId(messageBean.getSenderId());
        groupMessageBean.setRoomName(messageBean.getRoomName());
        groupMessageBean.setSenderAvatarLocalPath(messageBean.getSenderAvatarPath());
        groupMessageBean.setSignStatus(1);
        groupMessageBean.setSendTime(Timestamp.valueOf(messageBean.getSendTime()));
        groupMessageMapper.insert(groupMessageBean);
    }


}
